package com.fjwt.gz.proxy.ctrl.lite.banner;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.LiteBannerDTO;
import com.fjwt.gz.db.vo.LiteBannerVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.LiteBannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * 轮播图接口类
 */
@Slf4j
@RestController
@RequestMapping("/api/liteBanner")
public class LiteBannerController extends CommonCtrl {
    @Autowired
    private LiteBannerService liteBannerService;

    /**
     * 获取轮播图列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_LITE_BANNER_INFO')")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiRes getLiteBanner() {
        LiteBannerDTO dto = getObject(LiteBannerDTO.class);
        dto.setCurrentAgentNo(getCurrentAgentNo());
        log.info("机构端轮播图列表参数，请求地址：/api/liteBanner/list，请求参数：{}", JSONUtil.toJsonStr(dto));
        IPage<LiteBannerVO> page = liteBannerService.selectLiteBannerListPC(getIPage(), dto);
        log.info("机构端轮播图列表参数，请求地址：/api/liteBanner/list，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 轮播图新增
     */
    @PreAuthorize("hasAnyAuthority('ENT_LITE_BANNER_INFO_ADD')")
    @PostMapping("/add")
    public ApiRes liteBannerSave() {
        LiteBannerDTO dto = getObject(LiteBannerDTO.class);
        Long sysUserId = getCurrentUser().getSysUserId();
        log.info("机构端新增轮播图，请求地址：/api/liteBanner/add，请求参数：{}，sysUserId：{}", JSONUtil.toJsonStr(dto), JSONUtil.toJsonStr(sysUserId));
        liteBannerService.insert(dto,  sysUserId);
        return ApiRes.ok();
    }

    /**
     * 轮播图详情
     */
    @PreAuthorize("hasAnyAuthority('ENT_LITE_BANNER_INFO_VIEW')")
    @PostMapping("/detail")
    public ApiRes getBannerDetail() {
        Long bannerId = getValLong("bannerId");
        log.info("机构端轮播图详情参数，请求地址：/api/liteBanner/detail，请求参数：{}", JSONUtil.toJsonStr(bannerId));
        LiteBannerVO vo = liteBannerService.selectDetailById(bannerId);
        log.info("机构端轮播图详情参数，请求地址：/api/liteBanner/detail，返回结果：{}", JSONUtil.toJsonStr(vo));
        return ApiRes.ok(vo);
    }

    /**
     * 轮播图更新
     */
    @PreAuthorize("hasAnyAuthority('ENT_LITE_BANNER_INFO_EDIT')")
    @PostMapping("/update")
    public ApiRes updateBanner() {
        LiteBannerDTO dto = getObject(LiteBannerDTO.class);
        Long sysUserId = getCurrentUser().getSysUserId();
        log.info("机构端轮播图更新参数，请求地址：/api/liteBanner/update，请求参数：{},sysUserId:{}", JSONUtil.toJsonStr(dto), JSONUtil.toJsonStr(sysUserId));
        liteBannerService.updateBannerById(dto,sysUserId);
        return ApiRes.ok();
    }

    /**
     * 轮播图上下线
     */
    @PostMapping("/onOrOffLine")
    public ApiRes onOrOffLine() {
        LiteBannerDTO dto = getObject(LiteBannerDTO.class);
        Long sysUserId = getCurrentUser().getSysUserId();
        log.info("机构端轮播图上下线参数，请求地址：/api/liteBanner/onOrOffLine，请求参数：{}，sysUserId:{}", JSONUtil.toJsonStr(dto), JSONUtil.toJsonStr(sysUserId));
        liteBannerService.updateStateById(dto, sysUserId);
        return ApiRes.ok();
    }


    /**
     * 轮播图删除
     */
    @PreAuthorize("hasAnyAuthority('ENT_LITE_BANNER_INFO_DEL')")
    @PostMapping("/delBanner")
    public ApiRes delBanner() {
        Long bannerId = getValLong("bannerId");
        log.info("机构端轮播图删除参数，请求地址：/api/liteBanner/delBanner，请求参数：{}", JSONUtil.toJsonStr(bannerId));
        liteBannerService.updateIsDelById(bannerId);
        return ApiRes.ok();
    }
}
